#!/bin/bash

if [ ! -e "/usr/local/hestia/data/users/" ]; then
	exit
fi

###############################################################
#                Initialize functions/variables               #
###############################################################

source /etc/hestiacp/hestia.conf

# Load upgrade functions and variables
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# shellcheck source=/usr/local/hestia/func/upgrade.sh
source $HESTIA/func/upgrade.sh
source_conf "$HESTIA/conf/hestia.conf"

###############################################################
#             Set new version numbers for packages            #
###############################################################
# Hestia Control Panel
new_version=$(dpkg -l | awk '$2=="hestia" { print $3 }')

###############################################################
#               Begin standard upgrade routines               #
###############################################################

# Set default verbose output based on release branch
is_debug_build

# Detect operating system version
detect_os

# Initialize upgrade
prepare_upgrade_config

# Initialize backup directories
upgrade_init_backup

# Initialize logging
upgrade_init_logging

# Check if preinstall hook exists
if [ -e "/etc/hestiacp/hooks/pre_install.sh" ]; then
	/etc/hestiacp/hooks/pre_install.sh
fi

# Set up console display and welcome message
upgrade_welcome_message_log >> $LOG
upgrade_welcome_message

# Back up existing configuration files and templates
upgrade_start_backup | tee -a $LOG

# Perform upgrade health check and refresh configuration before upgrading
upgrade_health_check | tee -a $LOG
upgrade_replace_default_config | tee -a $LOG
upgrade_refresh_config | tee -a $LOG

# Execute version-specific upgrade scripts
upgrade_start_routine | tee -a $LOG

# Run upgrades of dependencies
source_conf "$HESTIA_BACKUP/upgrade.conf"

# Update Web domain templates
upgrade_rebuild_web_templates | tee -a $LOG

# Update Mail domain templates
upgrade_rebuild_mail_templates | tee -a $LOG

# Update DNS zone templates
upgrade_rebuild_dns_templates | tee -a $LOG

# Upgrade File Manager and update configuration
upgrade_filemanager | tee -a $LOG

# Upgrade SnappyMail if applicable
upgrade_snappymail | tee -a $LOG

# Upgrade Roundcube if applicable
upgrade_roundcube | tee -a $LOG

# Upgrade PHP dependencies
upgrade_dependencies | tee -a $LOG

# Update Cloudflare IPs if applicable
upgrade_cloudflare_ip | tee -a $LOG

# Upgrade phpMyAdmin if applicable
upgrade_phpmyadmin | tee -a $LOG

# Upgrade phpPgAdmin if applicable
upgrade_phppgadmin | tee -a $LOG

# Upgrade blackblaze-cli-took if applicable
upgrade_b2_tool | tee -a $LOG

# update whitelabel logo's
update_whitelabel_logo | tee -a $LOG

# Set new version number in hestia.conf
upgrade_set_version $new_version
upgrade_set_branch $new_version

# Perform upgrade clean-up tasks (rebuild domains and users, restart services, delete temporary files)
upgrade_cleanup_message | tee -a $LOG
upgrade_rebuild_users | tee -a $LOG
upgrade_restart_services | tee -a $LOG

# Add upgrade notification to admin user's panel and display completion message
upgrade_complete_message_log >> $LOG
upgrade_complete_message

# If setting variables are set to true in hestia.conf, send notifications and email output.
upgrade_send_notification_to_panel
upgrade_send_notification_to_email
upgrade_send_log_to_email

if [ -e "/etc/hestiacp/hooks/post_install.sh" ]; then
	/etc/hestiacp/hooks/post_install.sh
fi

exit 0
